package controller;

import javax.swing.event.TableModelEvent;
import manager.UserManager;
import view.UserDetailView;
import javax.swing.JOptionPane;

import java.awt.event.*;

import javax.swing.JList;

import entity.User;
import entity.state.user.ActiveState;
import entity.state.user.BlackListedState;
import entity.state.user.BlockedState;
import entity.state.user.UserState;

import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;


public class UserController {

    private view.UserListView userListView;
    private view.UserDetailView userDetailView;
    private UserManager userManager = new UserManager();

    public static void main(String args[]) {
        new UserController();
    }
    public MouseListener lsl = new MouseListener() {

        @Override
        public void mouseClicked(MouseEvent e) {
            String classname = e.getComponent().getClass().getSimpleName();
            if (classname.startsWith("JList")) {
                Object o = e.getSource();
                JList j = (JList) o;
                User user = userManager.findById(j.getSelectedIndex());
                userDetailView = new UserDetailView(user, tml);
            }
        }

        @Override
        public void mouseEntered(MouseEvent arg0) {
            // TODO Auto-generated method stub
        }

        @Override
        public void mouseExited(MouseEvent arg0) {
            // TODO Auto-generated method stub
        }

        @Override
        public void mousePressed(MouseEvent arg0) {
            // TODO Auto-generated method stub
        }

        @Override
        public void mouseReleased(MouseEvent arg0) {
            // TODO Auto-generated method stub
        }
    };
    TableModelListener tml = new TableModelListener() {

        public void tableChanged(TableModelEvent e) {
            User u;
            TableModel tm = (TableModel) e.getSource();
            u = (User) userManager.findById(new Integer((String) tm.getValueAt(0, 0)));
            UserState userState = u.getState();
            //
            switch (e.getColumn()) {
                case 1:
                    u.setVorName((String) tm.getValueAt(0, 1));
                    break;
                case 2:
                    u.setNachName((String) tm.getValueAt(0, 2));
                    break;
                case 3:
                    if (!(userState instanceof BlackListedState)) {// hier umbauen
                    	String st = (String)tm.getValueAt(0,3);
                    	if (st.equals("blocked")){
                    		u.setUserState(new BlockedState());// wird immer auf Blocked gesetzt
                    	}else if (st.equals("active")){
                    		u.setUserState(new ActiveState());
                    	}else if (st.equals("blacklisted")){
                    		u.setUserState(new BlackListedState());
                    	}
                    }else{
                    	JOptionPane.showMessageDialog(null,"Blacklisted User k�nnen den Status nicht mehr �ndern","UserBlackListed", JOptionPane.ERROR_MESSAGE);
                        /*try {
                            u.setUserState(new ActiveState());
                        } catch (Exception ex) {
                        	System.out.println("Blacklisted User bleiben blacklisted");
                        }*/
                    	//
                    }
                    break;
            }
        }
    };
    
}
